package com.bc.trade.channel.orangepay;

import java.util.SortedMap;
import java.util.TreeMap;

import com.alibaba.fastjson.JSONObject;
import com.bc.common.PayType;
import com.bc.common.CommonEnum.Resp;
import com.bc.common.message.DFReq;
import com.bc.common.message.DFResp;
import com.bc.trade.channel.IChannel;
import com.bc.trade.config.SystemConfig;
import com.bc.trade.message.QueryCardInfoReq;
import com.bc.trade.message.QueryCardInfoResp;
import com.bc.trade.message.QueryOrderResp;
import com.bc.trade.message.QuickPayReq;
import com.bc.trade.message.QuickPayResp;
import com.bc.trade.message.QuickPaySetReq;
import com.bc.trade.message.QuickPaySetResp;
import com.bc.trade.message.RefundReq;
import com.bc.trade.message.RefundResp;
import com.bc.trade.message.SendSMSReq;
import com.bc.trade.message.SendSMSResp;
import com.bc.trade.message.UnionPayReq;
import com.bc.trade.message.UnionPayResp;
import com.bc.trade.model.TradeOrder;
import com.fs.common.util.FSHttp;
import com.fs.common.util.FSJson;
import com.fs.common.util.FSLog;

public class OrangePayChannel implements IChannel {
	private static final String TRANSFER_URL = SystemConfig.channelConfig.get("orangepay_transfer_url");
	private static final String QUERY_URL = SystemConfig.channelConfig.get("orangepay_query_url");
	private static final String HM_APPID = SystemConfig.channelConfig.get("orangepay_hm_appid");
	private static final String HM_KEY = SystemConfig.channelConfig.get("orangepay_hm_key");
	private static final String CX_APPID = SystemConfig.channelConfig.get("orangepay_cx_appid");
	private static final String CX_KEY = SystemConfig.channelConfig.get("orangepay_cx_key");

	@Override
	public DFResp df(DFReq req, TradeOrder order) throws Exception {
		DFResp rsp = new DFResp(req);
		SortedMap<String, String> map = new TreeMap<String, String>();
		map.put("version", "1.0");
		if (PayType.DF_COMPANY.equals(req.getPayType())) {
			map.put("is_compay", "1");// 对公
		} else {
			map.put("is_compay", "0");// 对私
		}
		map.put("amount", order.getAmount().toString());
		map.put("customer_phone", req.getPhoneNo());
		map.put("product_id", req.getBalanceType());// 0203额度0201普通
		map.put("remark", req.getRemark());
		map.put("merchant_no", order.getOrderNo());
		map.put("customer_name", req.getAccountName());
		map.put("customer_cert_type", "01");
		map.put("customer_cert_id", req.getCertsNo());
		map.put("bank_no", req.getBankSettleNo());
		map.put("bank_name", req.getBankName());
		map.put("bank_card_no", req.getAccountCardNo());
		if ("OPHM".equals(order.getChannelCode())) {
			map.put("appid", HM_APPID);
			map.put("appkey", HM_KEY);
		} 
		if ("OPCX".equals(order.getChannelCode())) {
			map.put("appid", CX_APPID);
			map.put("appkey", CX_KEY);
		}
		map.put("channel", "N201");
		map.put("sign", OrangePayUtil.createSign(map));
		map.remove("appkey");
		FSLog.api.info("提交到orangepay代付数据：" + map.toString());
		try {
			String result = FSHttp.postJson(TRANSFER_URL, FSJson.toString(map));
			FSLog.api.info("orangepay响应：" + result);
			JSONObject returnObj = JSONObject.parseObject(result);
			String retcode = returnObj.getString("retcode");
			if ("0000".equals(retcode)) {
				rsp.setCodeAndMsg(Resp.Success.getCode(), Resp.Success.getMsg());
			} else if ("3000".equals(retcode)) {
				rsp.setCodeAndMsg(Resp.InProcess.getCode(), Resp.InProcess.getMsg());
			} else {
				rsp.setCodeAndMsg(Resp.Fail.getCode(), Resp.Fail.getMsg());
			}
		} catch (Exception e) {
			FSLog.api.error(e);
			rsp.setCodeAndMsg(Resp.InProcess.getCode(), Resp.InProcess.getMsg());
		}
		return rsp;
	}
	
	@Override
	public QueryOrderResp dfQuery(TradeOrder order) throws Exception {
		QueryOrderResp resp = new QueryOrderResp();
		SortedMap<String, String> map = new TreeMap<String, String>();
		map.put("version", "1.0");
		if ("OPHM".equals(order.getChannelCode())) {
			map.put("appid", HM_APPID);
			map.put("appkey", HM_KEY);
		} 
		if ("OPCX".equals(order.getChannelCode())) {
			map.put("appid", CX_APPID);
			map.put("appkey", CX_KEY);
		} 
		map.put("merchant_no", order.getOrderNo());
		map.put("query_id", "202");//余额代付
		map.put("sign", OrangePayUtil.createSign(map));
		map.remove("appkey");
		FSLog.api.info("提交到orangepay查询数据：" + map.toString());
		try {
			String result = FSHttp.postJson(QUERY_URL, FSJson.toString(map));
			FSLog.api.info("orangepay查询响应：" + result);
			JSONObject returnObj = JSONObject.parseObject(result);
			String retcode = returnObj.getString("retcode");
			if ("0000".equals(retcode)) {
				String trade_status = returnObj.getString("trade_status");
				if ("TRADE_SUCCESS".equals(trade_status)) {
					resp.setOriginalRespCode(Resp.Success.getCode());
					resp.setOriginalRespMsg(Resp.Success.getMsg());
					resp.setCodeAndMsg(Resp.Success.getCode(), Resp.Success.getMsg());
				} else if ("TRADE_FAI".equals(trade_status)) {
					resp.setOriginalRespCode(Resp.Fail.getCode());
					resp.setOriginalRespMsg(Resp.Fail.getMsg());
					resp.setCodeAndMsg(Resp.Success.getCode(), Resp.Success.getMsg());
				} else {
					resp.setOriginalRespCode(Resp.InProcess.getCode());
					resp.setOriginalRespMsg(Resp.InProcess.getMsg());
					resp.setCodeAndMsg(Resp.Success.getCode(), Resp.Success.getMsg());
				}
			} else {
				resp.setOriginalRespCode(Resp.InProcess.getCode());
				resp.setOriginalRespMsg(Resp.InProcess.getMsg());
				resp.setCodeAndMsg(Resp.Success.getCode(), Resp.Success.getMsg());
			}
			
		} catch (Exception e) {
			FSLog.api.error(e);
			resp.setOriginalRespCode(Resp.Fail.getCode());
			resp.setOriginalRespMsg(Resp.Fail.getMsg());
			resp.setCodeAndMsg(Resp.Fail.getCode(), Resp.Fail.getMsg());
		}
		return resp;
	}
	
	@Override
	public QueryOrderResp query(TradeOrder order) throws Exception {
		return null;
	}

	@Override
	public UnionPayResp unionpay(UnionPayReq req, TradeOrder order) throws Exception {
		return null;
	}

	@Override
	public QuickPaySetResp quickPaySet(QuickPaySetReq req, TradeOrder order) throws Exception {
		return null;
	}

	@Override
	public QuickPayResp quickPay(QuickPayReq req, TradeOrder order) throws Exception {
		return null;
	}

	@Override
	public SendSMSResp sendSMS(SendSMSReq req) throws Exception {
		return null;
	}

	@Override
	public QueryCardInfoResp queryCardInfo(QueryCardInfoReq req) throws Exception {
		return null;
	}

	@Override
	public RefundResp refund(RefundReq req, TradeOrder refundOrder, TradeOrder orignOrder) throws Exception {
		return null;
	}

}
